package com.zt.yongxinbao.service.impl;

import com.zt.yongxinbao.dto.PolicyParam;
import com.zt.yongxinbao.entity.UserPolicy;
import com.zt.yongxinbao.mapper.UserPolicyMapper;
import com.zt.yongxinbao.service.UserPolicyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class UserPolicyServiceImpl implements UserPolicyService {
    @Autowired
    private UserPolicyMapper userPolicyMapper;
    private UserPolicy userPolicy;

    @Override
    public List<PolicyParam> selectByUsername(String username){
        List<PolicyParam> userPolicyList=userPolicyMapper.selectByUsername(username);
        if (userPolicyList!=null && !userPolicyList.isEmpty()){
            return userPolicyList;
        }
        else return null;
    }

    @Override
    public Boolean updateById(Integer id,Integer time) {
        if (userPolicyMapper.updateById(id ,time)==1) return true;
        return false;
    }

    @Override
    public Boolean insert(UserPolicy userPolicy) {
        if (userPolicyMapper.insert(userPolicy)==1) return true;
        return false;
    }

    @Override
    public List<PolicyParam> selectUnvaildByUsername(String username) {
        List<PolicyParam> userPolicyList=userPolicyMapper.selectByUsername(username);
        if (userPolicyList!=null && !userPolicyList.isEmpty()){
            Iterator iterator=userPolicyList.iterator();
            while (iterator.hasNext()){
                PolicyParam policyParam= (PolicyParam) iterator.next();
                Date date=new Date();
                Long  a=  (policyParam.getEffectiveTime().getTime()+policyParam.getGoodsDeadline()*(24 * 60 * 60 * 1000)-date.getTime());
                if (a>0L) iterator.remove();
            }
            return userPolicyList;
        }
        else return null;
    }

    @Override
    public List<PolicyParam> selectWillvaildByUsername(String username) {
        List<PolicyParam> userPolicyList=userPolicyMapper.selectByUsername(username);
        if (userPolicyList!=null && !userPolicyList.isEmpty()){
            Iterator iterator=userPolicyList.iterator();
            while (iterator.hasNext()){
                PolicyParam policyParam= (PolicyParam) iterator.next();
                Date date=new Date();
                Long  a=  (policyParam.getEffectiveTime().getTime()+policyParam.getGoodsDeadline()*(24 * 60 * 60 * 1000)-date.getTime());
                if (a>5184000L||a<0) iterator.remove();
            }
            return userPolicyList;
        }
        else return null;
    }


}
